package com.gis.util;

import java.beans.PropertyVetoException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtils {
	private static String driver = "com.mysql.cj.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8";
	private static String username = "root";
	private static String password = "123456";
	private static ComboPooledDataSource ds = new ComboPooledDataSource();

	/**
	 * 读取src目录下的配置文件
	 * @param fileName
	 * @return
	 */
	public static Properties loadPropertyFile(String fileName) {
		if (null == fileName || fileName.equals("")) {
			throw new IllegalArgumentException("Properties file path can not be null: " + fileName);
		}

		InputStream inputStream = null;
		Properties properties = null;
		try {
			inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream(fileName);
			properties = new Properties();
			properties.load(inputStream);
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException("Properties file not found: " + fileName);
		} catch (IOException e) {
			throw new IllegalArgumentException("Properties file can not be loading: " + fileName);
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}

	/**
	 * c3p0数据库连接池
	 * @return
	 */
	public static DataSource getC3p0DateSource() {
		// 从配置文件读取数据库连接信息
		Properties properties = loadPropertyFile("jdbc.properties");
		try {
			ds.setDriverClass(properties.getProperty("jdbc.driver"));
		} catch (PropertyVetoException e1) {
			e1.printStackTrace();
		}
		ds.setJdbcUrl(properties.getProperty("jdbc.url"));
		ds.setUser(properties.getProperty("jdbc.username"));
		ds.setPassword(properties.getProperty("jdbc.password"));
		
		return ds;
	}
	
	/**
	 * druid数据库连接池
	 * @return
	 */
	public static DataSource getDruidDataSource() {
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		return ds;
	}

	/**
	 * 创建数据库连接
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Connection conn = ds.getConnection();
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 关闭数据库连接
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}